#include "page4_pid.h"
#include "ui_page4_pid.h"

page4::page4(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::page4)
{
    ui->setupUi(this);
    this->setFixedSize(1280,720);

//################################################
//#                                              #
//#           设置界面4背景                        #
//#           最后一次修改4.9，11：16               #
//#                                              #
//################################################
    QPixmap widget_background = QPixmap(":/ocean.jpg");
    widget_background = widget_background.scaled(1280,720,Qt::KeepAspectRatio);
    QPixmap temp_background(widget_background.size());
    temp_background.fill(Qt::transparent);
    //白色透明度
    //    QPainter picture_background(&temp_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    //    picture_background.drawPixmap(0,0,widget_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    //    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,150));
    //    picture_background.end();

    //    widget_background = temp_background;
    //黑色透明度
    QPainter picture_background(&widget_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    picture_background.drawPixmap(0,0,widget_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,210));
    picture_background.end();

    ui->label_background->setPixmap(widget_background);

//################################################
//#                                              #
//#           设置pid初始化                        #
//#           最后一次修改4.14，18：06              #
//#                                              #
//################################################
//init ui
    UI_Init();
//initialize the graph and pid
    Graph_Init();
    PID_Init();
//timer
    timer_PID_feedback.start(update_time);
    //timer_Environment_random.start(1000);
//button connect
    connect(ui->Apply_Button,&QPushButton::clicked,this,&page4::PID_Set);//apply the PID parameters on the LineEdit
    connect(ui->Stop_Button,&QPushButton::clicked,this,&page4::Stop);
//PID feedback
    connect(&timer_PID_feedback,&QTimer::timeout,this,[=]()
    {
        if(!start)
            return;
        p->PID_Update();
        Line_Update();
        //each update,update time+=1
    });
//set label style
    ui->label_1->setStyleSheet("color:rgb(255,255,255);");
    ui->label_2->setStyleSheet("color:rgb(255,255,255);");
    ui->label_3->setStyleSheet("color:rgb(255,255,255);");
    ui->label_4->setStyleSheet("color:rgb(255,255,255);");
    ui->label_5->setStyleSheet("color:rgb(255,255,255);");

    ui->label_D1->setStyleSheet("color:rgb(255,255,255);");
    ui->label_D2->setStyleSheet("color:rgb(255,255,255);");
    ui->label_D3->setStyleSheet("color:rgb(255,255,255);");
    ui->label_I1->setStyleSheet("color:rgb(255,255,255);");
    ui->label_I2->setStyleSheet("color:rgb(255,255,255);");
    ui->label_I3->setStyleSheet("color:rgb(255,255,255);");
    ui->label_P1->setStyleSheet("color:rgb(255,255,255);");
    ui->label_P2->setStyleSheet("color:rgb(255,255,255);");
    ui->label_P3->setStyleSheet("color:rgb(255,255,255);");


//################################################
//#                                              #
//#    窗口切换相关BUG + PID传输BUG：找不到槽函数     #
//#           最后一次修改4.14，18：41              #
//#                                              #
//################################################
    connect(ui->pushButton_page1,SIGNAL(clicked()),this,SLOT(on_pushButton_page1_clicked()));
    connect(ui->pushButton_page2,SIGNAL(clicked()),this,SLOT(on_pushButton_page2_clicked()));
    connect(ui->pushButton_page3,SIGNAL(clicked()),this,SLOT(on_pushButton_page3_clicked()));
    connect(ui->pushButton_page4,SIGNAL(clicked()),this,SLOT(on_pushButton_page4_clicked()));

    connect(ui->pushButton_send_pid,SIGNAL(clicked()),this,SLOT(on_pushButton_send_pid_clicked()));
}

page4::~page4()
{
    delete ui;
}

//################################################
//#                                              #
//#           窗口切换相关函数                      #
//#           最后一次修改4.9，7：51                #
//#                                              #
//################################################
void page4::on_pushButton_page1_clicked()
{
//    qDebug()<<"shit 1\n";
    emit pushbutton_page1_clicked();
}


void page4::on_pushButton_page2_clicked()
{
//    qDebug()<<"shit 2\n";
    emit pushbutton_page2_clicked();
}


void page4::on_pushButton_page3_clicked()
{
//    qDebug()<<"shit 3\n";
    emit pushbutton_page3_clicked();
}


void page4::on_pushButton_page4_clicked()
{
//    qDebug()<<"shit 4\n";
    emit pushbutton_page4_clicked();
}

//################################################
//#                                              #
//#           pid相关函数                         #
//#           最后一次修改4.14，18：06              #
//#                                              #
//################################################
void page4::PID_Set()
{
    start=true;
    //Init parameters
    p->Kp=ui->P_doubleSpinBox->text().toDouble();
    p->Ki=ui->I_doubleSpinBox->text().toDouble();
    p->Kd=ui->D_doubleSpinBox->text().toDouble();

    p->G_targetLocation=ui->target_value_LineEdit->text().toDouble();
    p->G_presentLocation=ui->current_value_LineEdit->text().toDouble();

    //graph init
    if(graph_has_been_print_once)
    {
        ui->customPlot->graph(0)->data().data()->clear();
        update_count=1;
        //Graph_Init();
    }
}

void page4::Line_Update()
{
    //make it visible
    ui->customPlot->legend->setVisible(true);
    ui->customPlot->replot();
    //draw
    ui->customPlot->graph(0)->addData(update_count*update_time,p->G_presentLocation);
    update_count++;

    if(abs(p->cur_error)<0.0001)
        Stop();
}

void page4::Graph_Init()
{
    //Initialize the graph
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setName("line One");

    ui->customPlot->xAxis->setLabel("Time/ms");
    ui->customPlot->yAxis->setLabel("Temperature/C");
    ui->customPlot->xAxis->setRange(0,update_time*100);//ms
    ui->customPlot->yAxis->setRange(0,150);//degree
}

void page4::Stop()//reinitialize the parameters
{
    start=false;
    graph_has_been_print_once=true;
    PID_Init();
}

void page4::PID_Init()
{
    p=new PID;//initialize the PID
}

void page4::UI_Init()
{
    ui->setupUi(this);
    setWindowTitle("PID Asistant");
    //spinbox
    ui->P_doubleSpinBox->setDecimals(2);
    ui->P_doubleSpinBox->setMinimum(0.0);
    ui->P_doubleSpinBox->setMaximum(10.0);
    ui->P_doubleSpinBox->setSingleStep(0.1);

    ui->I_doubleSpinBox->setDecimals(2);
    ui->I_doubleSpinBox->setMinimum(0.0);
    ui->I_doubleSpinBox->setMaximum(10.0);
    ui->I_doubleSpinBox->setSingleStep(0.1);

    ui->D_doubleSpinBox->setDecimals(2);
    ui->D_doubleSpinBox->setMinimum(0.0);
    ui->D_doubleSpinBox->setMaximum(10.0);
    ui->D_doubleSpinBox->setSingleStep(0.1);

}

//################################################
//#                                              #
//#           pid数据传输函数                      #
//#           最后一次修改4.14，18：06              #
//#                                              #
//################################################

void page4::on_pushButton_send_pid_clicked()
{
    qint32 p1 = (qint32)(ui->lineEdit_P1->text().toDouble()*10000);
    qint32 p2 = (qint32)(ui->lineEdit_P2->text().toDouble()*10000);
    qint32 p3 = (qint32)(ui->lineEdit_P3->text().toDouble()*10000);
    qint32 i1 = (qint32)(ui->lineEdit_I1->text().toDouble()*10000);
    qint32 i2 = (qint32)(ui->lineEdit_I2->text().toDouble()*10000);
    qint32 i3 = (qint32)(ui->lineEdit_I3->text().toDouble()*10000);
    qint32 d1 = (qint32)(ui->lineEdit_D1->text().toDouble()*10000);
    qint32 d2 = (qint32)(ui->lineEdit_D2->text().toDouble()*10000);
    qint32 d3 = (qint32)(ui->lineEdit_D3->text().toDouble()*10000);

    QByteArray data_send;

    data_send.resize(100);

    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x16;  //消息ID
    data_send[2] = 0x24;  //数据长度：36
    //传入P1的数据
    data_send[3] = p1;
    data_send[4] = p1>>8;
    data_send[5] = p1>>16;
    data_send[6] = p1>>24;
    //传入I1的数据
    data_send[7] = i1;
    data_send[8] = i1>>8;
    data_send[9] = i1>>16;
    data_send[10] = i1>>24;
    //传入D1的数据
    data_send[11] = d1;
    data_send[12] = d1>>8;
    data_send[13] = d1>>16;
    data_send[14] = d1>>24;

    //传入P2的数据
    data_send[15] = p2;
    data_send[16] = p2>>8;
    data_send[17] = p2>>16;
    data_send[18] = p2>>24;
    //传入I2的数据
    data_send[19] = i2;
    data_send[20] = i2>>8;
    data_send[21] = i2>>16;
    data_send[22] = i2>>24;
    //传入D2的数据
    data_send[23] = d2;
    data_send[24] = d2>>8;
    data_send[25] = d2>>16;
    data_send[26] = d2>>24;

    //传入P3的数据
    data_send[27] = p3;
    data_send[28] = p3>>8;
    data_send[29] = p3>>16;
    data_send[30] = p3>>24;
    //传入I3的数据
    data_send[31] = i3;
    data_send[32] = i3>>8;
    data_send[33] = i3>>16;
    data_send[34] = i3>>24;
    //传入D3的数据
    data_send[35] = d3;
    data_send[36] = d3>>8;
    data_send[37] = d3>>16;
    data_send[38] = d3>>24;

    //异或校验
    data_send[39] = data_send[1];
    for(int i = 2;i <= 38 ;i++){
        data_send[39] = data_send[39] ^ data_send[i];
    }

    data_send[40] = 0xFF; //数据尾

    emit data_upup->signal_send_Pid(data_send);
}

